package com.xc.hotelmgr.webapi.modules.dt.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xc.hotelmgr.webapi.common.utils.PageUtils;
import com.xc.hotelmgr.webapi.common.utils.Query;

import com.xc.hotelmgr.webapi.modules.dt.dao.DtDao;
import com.xc.hotelmgr.webapi.modules.dt.entity.DtEntity;
import com.xc.hotelmgr.webapi.modules.dt.service.DtService;


@Service("dtService")
public class DtServiceImpl extends ServiceImpl<DtDao, DtEntity> implements DtService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String name = (String)params.get("name");
        String dtId = params.get("dtId").toString();
        String inSql = "SELECT id FROM dt_dt WHERE FIND_IN_SET(id, getChild("+ dtId+ "))";
        IPage<DtEntity> page = this.page(
                new Query<DtEntity>().getPage(params),
                new QueryWrapper<DtEntity>().like(StringUtils.isNotBlank(name),"name", name).inSql("id",inSql)
        );

        return new PageUtils(page);
    }

    @Override
    public void setParentName(List<DtEntity> dts) {
        for (DtEntity curDt:dts) {
            for (DtEntity pdt : dts) {
                if (curDt.getParentId() == pdt.getId()) {
                    curDt.setParentName(pdt.getName());
                }
            }
        }
    }

}